package main

import (
	"fmt"
	"os"

	"simple_oa/dao"
	"simple_oa/routers"
	"simple_oa/settings"
)

func main() {
	if len(os.Args) < 2 {
		fmt.Println("No Config File Path!")
		return
	}
	// 加载配置文件
	if err := settings.Init(os.Args[1]); err != nil {
		fmt.Printf("load config from file failed, err:%v \n", err)
		return
	}
	// 连接数据库
	if err := dao.InitMySQL(settings.Conf.MySQLConfig); err != nil {
		fmt.Printf("init mysql failed, err:%v \n", err)
		return
	}
	defer dao.Close() // 程序退出关闭数据库连接
	// 模型绑定
	dao.InitModelMigrate()

	// 注册路由
	r := routers.SetupRouter()

	if err := r.Run(fmt.Sprintf(":%d", settings.Conf.Port)); err != nil {
		fmt.Printf("server startup failed, err:%v\n", err)
		return
	}
}
